package org.seoul.gtugs.client.ui;

import org.seoul.gtugs.client.PhotoUploadService;
import org.seoul.gtugs.client.PhotoUploadServiceAsync;
import org.seoul.gtugs.client.ui.DesktopPhotoUploadUI.DesktopPhotoUploadUIUiBinder;
import org.seoul.gtugs.shared.PhotoEntity;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;

public class DesktopPhotoUploadUI extends Composite implements FileUploadDisplay {

	private static DesktopPhotoUploadUIUiBinder uiBinder = GWT
			.create(DesktopPhotoUploadUIUiBinder.class);
	@UiField Image uploadedImage;
	@UiField Button btnUpload;
	@UiField FileUpload photoUploadForm;
	@UiField FormPanel uploadForm;
	
	PhotoUploadServiceAsync photoUploadService = GWT.create(PhotoUploadService.class);
	
	interface DesktopPhotoUploadUIUiBinder extends
			UiBinder<Widget, DesktopPhotoUploadUI> {
	}

	public DesktopPhotoUploadUI() {
		initWidget(uiBinder.createAndBindUi(this));
		btnUpload.setText("Upload");
		btnUpload.setText("Loading...");
		btnUpload.setEnabled(false);

		photoUploadForm.setName("image");
		
		startNewBlobstoreSession();
		
		uploadForm
		.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {

			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				uploadForm.reset();
				startNewBlobstoreSession();

				String key = event.getResults();
				
				//_debug = _debug + "uploaded key = " + key;
				
				GWT.log("uploaded key = " + key);
				//txtArea_debug.setText(_debug);
				
				photoUploadService.get(key,
						new AsyncCallback<PhotoEntity>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}

							@Override
							public void onSuccess(PhotoEntity result) {
								uploadedImage.setUrl(result.getServingUrl());

							}
						});
			}
		});				
	}

	public DesktopPhotoUploadUI(String firstName) {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public HasClickHandlers getUploadBtn() {
		// TODO Auto-generated method stub
		return null;
	}

	public Widget asWidget(){
		return this;
	}
	
	

private void startNewBlobstoreSession() {
	photoUploadService.getBlobstoreUploadUrl(new AsyncCallback<String>() {

		@Override
		public void onSuccess(String result) {
			//_debug = _debug + "Upload URL: = " + result;
			//txtArea_debug.setText(_debug);
			GWT.log("Upload URL: " + result);
			
			uploadForm.setAction(result);
			uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
			uploadForm.setMethod(FormPanel.METHOD_POST);

			btnUpload.setText("Upload");
			btnUpload.setEnabled(true);

		}

		@Override
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub

		}
	});
}

@UiHandler("btnUpload")
void onSubmit(ClickEvent e) {
	uploadForm.submit();
}	
	
}

